package com.hwy.jmh.json.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hwy.jmh.json.Json;

/**
 * @Author hweiyu
 * @Description
 * @Date 2022/2/7 17:19
 */
public class JacksonImpl implements Json {

    private ObjectMapper mapper = new ObjectMapper();

    @Override
    public String toStr(Object obj) {
        try {
            return mapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Object toBean(String str, Class cls) {
        try {
            return mapper.readValue(str, cls);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }
}
